import IxChariot
import time
import sys
import json

class IxChariotTestLibrary:
	def __init__(self):
		self.ixinfo = IxChariot.IxChariotInfo()
		#self.ixinfo = None
	
	def init_IxChariotInfo(self):
		self.ixinfo = IxChariot.IxChariotInfo()
		return self.ixinfo 
	
	def setParameterConfig(self, script, pairNum, srcip, dstip, duration, path):
		ret = self.ixinfo.setParameterConfig(str(script), int(pairNum), str(srcip), str(dstip), int(duration), str(path))
		return ret
		
	def runIxChariot(self):
		self.ixinfo.runIxChariot()

	def stopIxChariot(self):
		self.ixinfo.stopIxChariot()

	def readyToGetData(self):
		self.ixinfo.readyToGetDate()

	def endToGetData(self):
		self.ixinfo.endToGetData()

	def waitUntilGetReady(self, count=50):
		ret = -1
		while (ret != 1):
			ret = self.ixinfo.isGetReady()
			time.sleep(0.25)
			count = count - 1
			#print 'count %d\n' % count
			if count <= 0:
				break
		return ret
   	def waitUntilGetReadyALL(self, count=80):
		ret = -1
		while (ret < 1):
			ret = self.ixinfo.isGetReady_ALL()
			print ret
			time.sleep(0.25)
			count = count - 1
			#print 'count %d\n' % count
			if count <= 0:
				break
			if ret == 2:
				break
			elif ret == 3:
				break
		return ret
   	def getMaxThroughput_ALL(self):
   		return self.ixinfo.getMaxThroughput_ALL()

	def getMinThroughput_ALL(self):
   		return self.ixinfo.getMinThroughput_ALL()

   	def getAvgThroughput_ALL(self):
   		return self.ixinfo.getAvgThroughput_ALL()

   	def getMaxResponseTime_ALL(self):
   		return self.ixinfo.getMaxResponseTime_ALL()

   	def getMinResponseTime_ALL(self):
   		return self.ixinfo.getMinResponseTime_ALL()

   	def getAvgResponseTime_ALL(self):
   		return self.ixinfo.getAvgResponseTime_ALL()	

   	def getMaxThroughput(self):
   		return self.ixinfo.getMaxThroughput()

   	def getMinThroughput(self):
   		return self.ixinfo.getMinThroughput()

	def getAvgThroughput(self):
   		return self.ixinfo.getAvgThroughput()

	def getMaxResponseTime(self):
   		return self.ixinfo.getMaxResponseTime()

	def getMinResponseTime(self):
   		return self.ixinfo.getMinResponseTime()

	def getAvgResponseTime(self):
   		return self.ixinfo.getAvgResponseTime()		
	
if __name__ == '__main__':
	'''
	if (len(sys.argv) != 2):
		raise Exception("input parameter != 2")
	'''
	
	#ixcharoit_filename = str(sys.argv[1])+ 'test.tst'
	ixcharoit_filename = str(sys.argv[1])
	#ixcharoit_filename = './test.tst'
	avg_throughput_filename = str(sys.argv[2])	
	#avg_throughput_filename = 'tp.txt'
	
	json_str = str(sys.argv[3])
	print json_str
	#json_str = '{"duration":"30", "pairs":[{"scripts":"Throughput.scr", "end1":"192.168.0.5", "end2":"192.168.0.18", "num":"6"}]}'
	json_obj = json.loads(json_str.replace("'",'"'))
	print json_obj
	#path = json_obj['path']

	duration = int(json_obj['duration'])
	print duration
	pair_array = json_obj['pairs']
	
	ix = IxChariotTestLibrary()

	for pair in pair_array:
		scripts = str(pair['scripts'])
		end1 = str(pair['end1'])
		end2 = str(pair['end2'])
		num = int(pair['num'])
		print (scripts, end1, end2, num)
		ret = ix.setParameterConfig(scripts, num, end1, end2, duration, ixcharoit_filename)
	ix.runIxChariot()
	print 'test end1'
	time.sleep(duration + 10)
	#print 'readyToGetData'
	'''
	ix.readyToGetData()
	time.sleep(5)
	print 'endToGetData'
	ix.endToGetData()
	ix.waitUntilGetReady()
	ret = ix.getMaxThroughput()
	print 'getMaxThroughput: %f' % ret 
	print 'getMinThroughput: %f' % ix.getMinThroughput()
	print 'test end2'
	time.sleep(6)
	'''
	#ix.stopIxChariot()
	#ret = ix.waitUntilGetReadyALL()
	#print 'ret %d\n' % ret
	avg_throughput = ix.getAvgThroughput_ALL()
	print 'getMaxThroughput_ALL: %f' % ix.getMaxThroughput_ALL()
	print 'getMinThroughput_ALL: %f' % ix.getMinThroughput_ALL()
	print 'getAvgResponseTime_ALL: %f' % ix.getAvgResponseTime_ALL()

	fb = open(avg_throughput_filename, 'w+')
	fb.write('avg_throughput: ' + str(avg_throughput) +'\n')
	#fb.write('waitUntilGetReadyALL: ' + str(ret) +'\n')  
	fb.close()
	print 'test over'
	time.sleep(5)